package com.wjy.simple.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.wjy.sfhcore.util.DataUtil;

/**
 * SharedPreferences数据存取工厂
 *
 * @author wjy
 */
public class SPHelper {
    private SharedPreferences sp;
    private Editor editor;
    private DataUtil mDataUtil = new DataUtil();
    private Context context;

    private static SPHelper instance = new SPHelper();

    private SPHelper() {
    }

    public static SPHelper getInstance() {
        return instance;
    }

    public void init(Context context) {
        this.context = context;
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public SharedPreferences getSharedPreferences() {
        return sp;
    }

    public Editor getEditor() {
        return editor;
    }

    public void registerOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        sp.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }

    public void unregisterOnSharedPreferenceChangeListener(
            SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        sp.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }


    public String getString() {
        return sp.getString("str", "default");
    }

    public void saveString(String text) {
        editor.putString("str", text);
        editor.commit();
    }
}
